package com.yy.wga.goods.service.manager;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yy.wga.common.core.dto.Result;
import com.yy.wga.common.core.enmus.EUserGrade;
import com.yy.wga.common.core.exception.BusinessException;
import com.yy.wga.common.util.CommissionUtil;
import com.yy.wga.common.util.mapping.BeansUtils;
import com.yy.wga.goods.feign.request.FeedReq;
import com.yy.wga.goods.feign.request.LiveReq;
import com.yy.wga.goods.feign.request.TalentReq;
import com.yy.wga.goods.feign.response.FeedResp;
import com.yy.wga.goods.feign.response.LiveResp;
import com.yy.wga.goods.feign.response.TalentResp;
import com.yy.wga.goods.service.api.request.feed.GetFeedListReq;
import com.yy.wga.goods.service.api.request.feed.GetFeedPromotionInfoReq;
import com.yy.wga.goods.service.api.request.live.GetLiveNoticeRecordListReq;
import com.yy.wga.goods.service.api.request.live.GetLiveNoticeRecordQrCodeReq;
import com.yy.wga.goods.service.api.request.live.GetLiveRecordListReq;
import com.yy.wga.goods.service.api.request.live.GetLiveRecordQrCodeReq;
import com.yy.wga.goods.service.api.request.telent.GetBindTalentListReq;
import com.yy.wga.goods.service.api.response.bind.BindTalentListResp;
import com.yy.wga.goods.service.api.response.feed.FeedListResp;
import com.yy.wga.goods.service.api.response.feed.FeedPromotionInfoResp;
import com.yy.wga.goods.service.api.response.live.LiveNoticeRecordListResp;
import com.yy.wga.goods.service.api.response.live.LiveNoticeRecordQrCodeResp;
import com.yy.wga.goods.service.api.response.live.LiveRecordListResp;
import com.yy.wga.goods.service.api.response.live.LiveRecordQrCodeResp;
import com.yy.wga.goods.service.service.PromoterService;
import com.yy.wga.user.feign.api.UserWechatFeign;
import com.yy.wga.user.feign.request.UserWechatListRequest;
import com.yy.wga.user.feign.response.UserWechatInfoResponse;
import com.yy.wga.wechat.feign.api.WechatMpFeign;
import com.yy.wga.wechat.feign.response.WechatGetAccessTokenResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class TalentManager {

    @Resource
    private PromoterService promoterService;
    @Resource
    private UserWechatFeign userWechatFeign;
    @Resource
    private WechatMpFeign wechatMpFeign;

    @Value("${mp.default.live.talentAppid:}")
    private String DEFAULT_LIVE_TALENT_APPID;

//    public void getLiveCommissionProductList(){
//
//        GetLiveCommissionProductListReq req = new GetLiveCommissionProductListReq();
//        req.setPageSize(1);
//        req.setTalentAppid(DEFAULT_LIVE_TALENTAPPID);
//
//        LiveCommissionProductListResp liveCommissionProductList = promoterService.getLiveCommissionProductList(req);
//        List<LiveCommissionProductListResp.Product> productList = liveCommissionProductList.getProductList();
//        if (!CollectionUtils.isEmpty(productList)){
//
//        }
//    }

    public FeedResp.FeedList getFeedList(FeedReq.GetFeedList req) {
        GetFeedListReq getFeedListReq = new GetFeedListReq();
        BeanUtils.copyProperties(req, getFeedListReq);

        FeedListResp feedListResp = promoterService.getFeedList(getFeedListReq);
        List<FeedListResp.Feed> feedList = feedListResp.getFeedList();


        List<FeedResp.FeedInfo> resultList = new ArrayList<>();
        for (FeedListResp.Feed feed : feedList) {

            FeedListResp.ProductInfo productInfoResp = feed.getProductInfo();
            FeedResp.ProductInfo productInfo = new FeedResp.ProductInfo();
            productInfo.setProductId(String.valueOf(productInfoResp.getProductId()));
            productInfo.setProductName(productInfoResp.getProductName());
            productInfo.setProductImgUrl(productInfoResp.getProductImgUrl());
            productInfo.setProductPrice(productInfoResp.getProductMiniPrice());
            EUserGrade userGrade = EUserGrade.getByType(req.getGrade(), req.getLevel());
            Integer commissionValue = CommissionUtil.calcCommissionValue(productInfoResp.getProductMiniPrice(), productInfoResp.getCommissionRatio(), userGrade);
            productInfo.setCommissionValue(commissionValue);


            FeedResp.FeedInfo feedInfo = new FeedResp.FeedInfo();
            feedInfo.setExportId(feedInfo.getExportId());
            feedInfo.setProductInfo(productInfo);

            resultList.add(feedInfo);
        }

        FeedResp.FeedList resp = new FeedResp.FeedList();
        resp.setHasMore(feedListResp.isHasMore());
        resp.setNextKey(feedListResp.getNextKey());
        resp.setFeedList(resultList);
        return resp;

    }

    public FeedResp.FeedPromotionInfo getFeedPromotionInfo(FeedReq.GetFeedPromotionInfoReq req) {
        GetFeedPromotionInfoReq getFeedPromotionInfoReq = new GetFeedPromotionInfoReq();
        BeansUtils.copyProperties(req, getFeedPromotionInfoReq);
        getFeedPromotionInfoReq.setMiniProgramAppid(this.getMiniProgramAppid());
        FeedPromotionInfoResp feedPromotionInfo = promoterService.getFeedPromotionInfo(getFeedPromotionInfoReq);
        return BeansUtils.map(feedPromotionInfo, FeedResp.FeedPromotionInfo.class);
    }


    public TalentResp.BindTalentList getBindTalentList(TalentReq.GetBindTalentList req) {
        GetBindTalentListReq getBindTalentListReq = new GetBindTalentListReq();
        BeansUtils.copyProperties(req, getBindTalentListReq);
        BindTalentListResp bindTalentList = promoterService.getBindTalentList(getBindTalentListReq);
        return BeansUtils.map(bindTalentList, TalentResp.BindTalentList.class);
    }

    public LiveResp.LiveRecordList getLiveRecordList(LiveReq.GetLiveRecordList req) {
        GetLiveRecordListReq getLiveRecordListReq = new GetLiveRecordListReq();
        BeansUtils.copyProperties(req, getLiveRecordListReq);
        getLiveRecordListReq.setMiniProgramAppid(this.getMiniProgramAppid());
        LiveRecordListResp liveRecordList = promoterService.getLiveRecordList(getLiveRecordListReq);
        return BeansUtils.map(liveRecordList, LiveResp.LiveRecordList.class);
    }


    public LiveResp.LiveInfo getOneLive(LiveReq.GetOneLive req) {
        // 先获取开播列表 看看是否有直播间
        GetLiveRecordListReq getFeedPromotionInfoReq = new GetLiveRecordListReq();
        BeansUtils.copyProperties(req, getFeedPromotionInfoReq);
        getFeedPromotionInfoReq.setMiniProgramAppid(this.getMiniProgramAppid());
        LiveResp.LiveInfo liveInfo = new LiveResp.LiveInfo();
        LiveRecordListResp liveRecordListResp = promoterService.getLiveRecordList(getFeedPromotionInfoReq);
        List<LiveRecordListResp.LiveRecord> liveRecordList = liveRecordListResp.getLiveRecordList();
        if (!CollectionUtils.isEmpty(liveRecordList)) {
            LiveRecordListResp.LiveRecord liveRecord = liveRecordList.get(0);
            // 直播中
            liveInfo.setStatus(2);
            liveInfo.setExportId(liveRecord.getExportId());
            liveInfo.setDescription(liveRecord.getDescription());
            liveInfo.setPromoterShareLink(liveRecord.getPromoterShareLink());
            return liveInfo;
        }
        // 获取预约直播间
        GetLiveNoticeRecordListReq getLiveNoticeRecordListReq = new GetLiveNoticeRecordListReq();
        getLiveNoticeRecordListReq.setTalentAppid(req.getTalentAppid());
        LiveNoticeRecordListResp liveNoticeRecordListResp = promoterService.getLiveNoticeRecordList(getLiveNoticeRecordListReq);
        List<LiveNoticeRecordListResp.LiveNoticeRecord> liveNoticeRecordList = liveNoticeRecordListResp.getLiveNoticeRecordList();
        if (!CollectionUtils.isEmpty(liveNoticeRecordList)) {
            LiveNoticeRecordListResp.LiveNoticeRecord liveNoticeRecord = liveNoticeRecordList.get(0);
            // 可预约
            liveInfo.setStatus(1);
            liveInfo.setNoticeId(liveNoticeRecord.getNoticeId());
            liveInfo.setDescription(liveNoticeRecord.getDescription());
            liveInfo.setStartTime(new Date(liveNoticeRecord.getStartTime()));
            return liveInfo;
        }
        // 无直播
        liveInfo.setStatus(0);
        return liveInfo;
    }

    public LiveResp.LiveShareQcCode getLiveShareQrCode(LiveReq.GetLiveShareQcCode req) {
        String exportId = req.getExportId();
        String noticeId = req.getNoticeId();
        if (StringUtils.isBlank(exportId) && StringUtils.isBlank(noticeId)) {
            throw new BusinessException("直播间ID或预约ID为空,无法生成二维码");
        }
        // 查询用户的shareAppId
        UserWechatListRequest userWechatListRequest = new UserWechatListRequest();
        userWechatListRequest.setOpenId(req.getOpenId());
        Result<List<UserWechatInfoResponse>> wechatInfoResult = userWechatFeign.list(userWechatListRequest);
        if (!wechatInfoResult.isSuccess()) {
            throw new BusinessException("获取用户信息失败");
        }
        List<UserWechatInfoResponse> userWechatList = wechatInfoResult.getData();
        if (userWechatList.isEmpty()) {
            throw new BusinessException("未查询到用户");
        }
        UserWechatInfoResponse userWechatDO = userWechatList.get(0);
        String sharerAppid = userWechatDO.getSharerAppid();

        LiveResp.LiveShareQcCode liveShareQcCodeResp = new LiveResp.LiveShareQcCode();
        if (StringUtils.isBlank(sharerAppid)) {
            // 需要走注册绑定流程完成 得到sharerAppid
            liveShareQcCodeResp.setNeedRegisterAndBind(true);
            return liveShareQcCodeResp;
        }
        if (StringUtils.isNotBlank(exportId)) {
            GetLiveRecordQrCodeReq getLiveRecordQrCodeReq = new GetLiveRecordQrCodeReq();
            BeansUtils.copyProperties(req, getLiveRecordQrCodeReq);
            getLiveRecordQrCodeReq.setSharerAppid(sharerAppid);
            LiveRecordQrCodeResp liveRecordQrCode = promoterService.getLiveRecordQrCode(getLiveRecordQrCodeReq);
            liveShareQcCodeResp.setQrCodeUrl(liveRecordQrCode.getQrCodeUrl());
            liveShareQcCodeResp.setType(1);
        } else {
            GetLiveNoticeRecordQrCodeReq getLiveRecordQrCodeReq = new GetLiveNoticeRecordQrCodeReq();
            BeansUtils.copyProperties(req, getLiveRecordQrCodeReq);
            getLiveRecordQrCodeReq.setSharerAppid(sharerAppid);
            LiveNoticeRecordQrCodeResp liveNoticeRecordQrCode = promoterService.getLiveNoticeRecordQrCode(getLiveRecordQrCodeReq);
            liveShareQcCodeResp.setQrCodeUrl(liveNoticeRecordQrCode.getQrCodeUrl());
            liveShareQcCodeResp.setType(2);
        }
        return liveShareQcCodeResp;
    }


    public String getMiniProgramAppid() {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        return getAccessTokenResponse.getAppId();
    }
}
